package db

import (
	"database/sql"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
)

var (
	dbConn *sql.DB
	err    error
)

func ConnectDB(userName string, password string, dbName string, dbUrl string) error {
	dataSourceName := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8&parseTime=true&loc=Local",
		userName, password, dbUrl, dbName)
	fmt.Println("dataSourceName:", dataSourceName)
	dbConn, err = sql.Open("mysql", dataSourceName)
	if err != nil {
		return err
	}
	dbConn.SetMaxOpenConns(2000)
	dbConn.SetMaxIdleConns(1000)
	err := dbConn.Ping()
	return err
}

func DisconnectDB() error {
	if dbConn != nil {
		return dbConn.Close()
	}
	return nil
}
